package BarCodeAndBookDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bookDao.BookDbconnection;
import table.BarCodeAndBook;

public class BarCodeAndBookImpl implements BarCodeAndBookDao{
	String sql = null;
	int result = 0;
	/**
	 *通过图书唯一的编号去查找图书的信息 用于放在借书界面的表中
	 */
	@Override 
	public List<BarCodeAndBook> getBookByBarCode(int x) {
		Connection con = null;
		PreparedStatement ps = null; 
		ResultSet rs = null;
		List<BarCodeAndBook> BarCodeAndBook = new ArrayList<BarCodeAndBook>();
		try {
			con = BookDbconnection.getConnection();
			sql = "SELECT book_id,book_name,book_writer,book_publishingHouse,callNumber,book_price,book_status,book.book_isbn FROM bookbarcode,book WHERE book.book_isbn=bookbarcode.book_isbn AND barCodeId=?";
			ps = con.prepareStatement(sql);
			ps.setInt(1, x);
			rs = ps.executeQuery(); 
			while(rs.next()) { 
				int bookId = rs.getInt(1);
				String bookName = rs.getString(2);
				String bookWriter = rs.getString(3);
				String bookPublishingHouse = rs.getString(4);
				String callNumber = rs.getString(5);// 索书号
				double bookPrice = rs.getDouble(6);
				String bookStatus = rs.getString(7);
				String bookIsbn = rs.getString(8);// 图书编号 
				BarCodeAndBook b = new BarCodeAndBook(bookId,bookName,bookWriter,bookPublishingHouse,callNumber,bookPrice,bookIsbn,bookStatus,x);
				BarCodeAndBook.add(b);
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}finally {
			BookDbconnection.close(rs, ps);
		}
		return BarCodeAndBook;
	}
	@Override
	public void updateBookStatus(String x, int y) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = BookDbconnection.getConnection();
			sql = "UPDATE bookbarcode SET book_status=? WHERE barCodeId=?";
			ps = con.prepareStatement(sql);
			ps.setString(1, x); 
			ps.setInt(2, y); 
			result = ps.executeUpdate();
			System.out.println("操作结果是" + result);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BookDbconnection.close(rs, ps);
		}
		
	}
	@Override
	public void saveBookBarCode(String x, String y) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = BookDbconnection.getConnection();
			sql = "INSERT INTO bookbarcode VALUE(null,?,?)";
			ps = con.prepareStatement(sql);
			ps.setString(1, x);
			ps.setString(2, y);
			result = ps.executeUpdate(); 
			System.out.println("操作结果是" + result);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BookDbconnection.close(rs, ps);
		}
		
	}
	@Override
	public boolean findBookStatus(String x) {
		Connection con = null;
		PreparedStatement ps = null; 
		ResultSet rs = null;
		try {
			con = BookDbconnection.getConnection();
			sql = "SELECT book_status FROM bookbarcode WHERE book_isbn=?";
			ps = con.prepareStatement(sql);
			ps.setString(1, x);
			rs = ps.executeQuery(); 
			while(rs.next()) { 
				while(rs.getString(1).equals("借出")) {
					return false;
				}
			}

		}
		catch(SQLException e) {
			e.printStackTrace();
		}finally {
			BookDbconnection.close(rs, ps);
		}
		return true;
		
	} 
	public int findBookNumber(String x) {
		Connection con = null; 
		PreparedStatement ps = null;
		ResultSet rs = null;
		int i = 0;  
		try {
			con = BookDbconnection.getConnection();
			sql = "SELECT barCodeId FROM bookbarcode WHERE book_isbn=?";
			ps = con.prepareStatement(sql);
			ps.setString(1, x);
			rs = ps.executeQuery();
			while (rs.next()) {
				// int borrowingInformationId = rs.getInt(1);
				i++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BookDbconnection.close(rs, ps);
		}
		return i;
	}
}
